<?php
	$leftIndent = 20;
	$pageWidth = 800;
	$width = $pageWidth - $leftIndent;
	
	$root = "/";
	$decoration = $root."Decoration";
	$empty = $root."phylotype/images/empty.gif";
	$peopleIcon = $root."Decoration/Icons/Boffins.gif";
	$personIcon = $root."Decoration/Icons/Boffins.gif";
	$researchIcon = $root."Decoration/Icons/Flask.gif";
	$publicationsIcon = $root."Decoration/Icons/Books.gif";
	$softwareIcon = $root."Decoration/Icons/Cog.gif";
	$dataIcon = $root."Decoration/Icons/Filing.png";
	$linksIcon = $root."Decoration/Icons/Network.png";
	$mapsIcon = $root."Decoration/Icons/Map.gif";
	$jobsIcon = $root."Decoration/Icons/MonkeyTypewriter.png";
	
	function openDocument($title) {
		echo('
			<html>
				<head>
					<title>Oxford Evolutionary Biology - '.$title.'</title>
					<link href="evolve.css" rel="stylesheet" media="screen">
				</head>
			
			<body>
		');
	}

	function closeDocument() {
		echo('
					<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
					</script>
					<script type="text/javascript">
						_uacct = "UA-277246-1";
						urchinTracker();
					</script>
				</body>
			</html> 
		');
	}

	function drawHeader($class="grey-box") {
		global $empty;
		global $headingIndent;
		global $pageWidth;
		global $leftIndent;

		echo('
		<table class="'.$class.'" width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td><img src="'.$empty.'" alt="" height="5" width="10" border="0"></td>
			</tr>
			<tr>
				<td class="box-bottom"></td>
			</tr>
		</table>
		<table width="'.$pageWidth.'" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td width="'.$leftIndent.'"><img src="'.$empty.'" alt="" height="20" width="'.$leftIndent.'" border="0"></td>
				<td width="53" nowrap>
					<a href="http://www.ox.ac.uk/"> 
					<img src="/phylotype/images/logotree.png" alt="" width="79" height="58" align="left" border="0">
					</a>
				</td>
				<td valign="middle" nowrap>
					<span class="minor-title">
		
					</span><br>
					<span class="major-title">
						<a href="http://hivweb.sanbi.ac.za/phylotype/cgi-bin/phylotypes.php?phylotype=1">HIV-1 Phylotypes Description</a>
					</span>
				</td>
				<td valign="bottom"><img src="'.$empty.'" alt="" height="20" width="32" border="0"></td>
			</tr>
		</table>
		');
	}

	function navigationButton($label, $link, $class="button") {
		global $empty;
		global $root;
		return	'<div align="center">
					<table class="'.$class.'" width="80%" border="0" cellspacing="0" cellpadding="0">
						<tr>
							<td class="button-left">
								<span link="'.$root.$link.'"></span>
							</td>
							<td nowrap class="button-fill" align="center">
								<a href="'.$root.$link.'">
									<span class="button-title" >'.$label.'</span>
								</a>
							</td>
							<td class="button-right">
								<a href="'.$root.$link.'">
									<img src="'.$empty.'" alt="" height="5" width="10" border="0">
								</a>
							</td>
						</tr>
					</table>
				</div>';
	}

	function drawNavigationBar($page="", $class="grey-box") {
		global $headingIndent;
		global $pageWidth;

		// if $page equals the button title then the button is disabled
		// as we are already on that page.
		echo('
			<table class="'.$class.'" width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td class="box-top" style="height:12px"></td>
			</tr>
			<tr>
				<td>
					<table width="'.$pageWidth.'" border="0" cellspacing="0" cellpadding="0">
						<tr>
							<td width="18"><img src="/phylotype/images/empty.gif" alt="" height="20" width="18"></td>
							<td width="110">' .
								navigationButton("Home", "", ($page == "Home" ? "button-disabled" : "button")) .
							'</td>
							<td width="110">' .
								navigationButton("People", "people.html", ($page == "People" ? "button-disabled" : "button")) .
							'</td>
							<td width="110">' .
								navigationButton("Research", "research.html", ($page == "Research" ? "button-disabled" : "button")) .
							'</td>
							<td width="110">' .
								navigationButton("Publications", "publications.html", ($page == "Publications" ? "button-disabled" : "button")) .
							'</td>
							<td width="110">' .
								navigationButton("Software", "software.html", ($page == "Software" ? "button-disabled" : "button")) .
							'</td>
							<td width="110">' .
								navigationButton("Data", "data.html", ($page == "Data" ? "button-disabled" : "button")) .
							'</td>
							<td width="110">' .
								navigationButton("Links", "links.html", ($page == "Links" ? "button-disabled" : "button")) .
							'</td>
						</tr>
					</table>
				</td>
			</tr>
			<tr height="20">
				<td height="20" class="box-bottom"><img src="/phylotype/images/empty.gif" alt="" height="13" width="10" border="0"></td>
			</tr>
		</table>');
	}
	
	function drawTitleBar($title, $icon, $subtitle = NULL, $class = "blue-box") {
		global $leftIndent;
		global $pageWidth;
		
		echo('
			<table width="'.$pageWidth.'" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td width="'.$leftIndent.'"><img src="/phylotype/images/empty.gif" alt="" height="20" width="'.$leftIndent.'"></td>
					<td>
		');
		echo(openBox($class));
		echo(openBoxRow("64"));
		echo('
			<table height="64" width="100%">
				<tr>
					<td width="64">
						<img src="'.$icon.'" alt="" width="64" height="64" border="0">
					</td>
					<td halign="left">
						<div class="major-title">' . $title . '</div>');
		if ($subtitle) {
			echo('<div class="box-body">' . $subtitle . '</div>');
		}	
						
		echo('		</td>
				</tr>
			</table>
		');
		echo(closeBoxRow());
		echo(closeBox());
		echo('
				</td>
			</tr>
		</table>'
		);
	}
	
	function drawFooter($author, $class="grey-box") {
		echo('
		<table class="'.$class.'" width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr height="20">
				<td background="/Decoration/Grey/GreyBox/Top.gif"><img src="/phylotype/images/empty.gif" alt="" height="14" width="10" border="0"></td>
			</tr>
			<tr>
				<td valign="top">
					<table width="800" border="0" cellspacing="0" cellpadding="0">
						<tr>
							<td>
								<div class="box-extra" align="center">
									Page layout last updated ' . date("d F Y ", getlastmod())  . ' by ' . 
									$author . '</div>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>');
	}
						
	function openPageBody() {
		global $leftIndent;
		global $pageWidth;
		
		return '
			<table width="'.$pageWidth.'" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td width="'.$leftIndent.'"><img src="/phylotype/images/empty.gif" alt="" height="20" width="'.$leftIndent.'"></td>
				';
	}
	
	function openColumn($width = "100%") {
		return '
				<td valign="top" width="'.$width.'">
				';
	}

	function closeColumn() {
		return '</td>';
	}

	function closePageBody() {
		return '
					</tr>
				</table>
				';
	}

	function button($label, $link, $class="button", $width="auto", $align="right", $valign="middle") {
		return	'<div align="'. $align .'" valign="'.$valign.'">
					<table border="0" cellspacing="0" cellpadding="0"><tr><td>
					<a href="'.$link.'">
					<table class="'.$class.'" width="'.$width.'" border="0" cellspacing="0" cellpadding="0">
						<tr>
							<td class="button-left"></td>
							<td nowrap class="button-fill">
								<a href="'.$link.'">
									<div class="button-title" align="center">'.$label.'</div>
								</a>
							</td>
							<td class="button-right"></td>
						</tr>
					</table>
				</a></td></tr></table></div>';
	}

	function iconButton($label, $icon, $link, $class="button", $width="auto", $align="right") {
		return	'<div align="'. $align .'">
					<table border="0" cellspacing="0" cellpadding="0"><tr><td>
					<a href="'.$link.'">
					<table class="'.$class.'" width="'.$width.'" border="0" cellspacing="0" cellpadding="0">
						<tr>
							<td class="button-left"></td>
							<td nowrap class="button-fill">
								<table><tr>
								<td>
								<a href="'.$link.'">
									<img src="'.$icon.'" height="14" width="14">
								</a></td>
								<td><a href="'.$link.'">
									<div class="button-title" align="center">'.$label.'</div></td>
								</a></tr></table>
							</td>
							<td class="button-right"></td>
						</tr>
					</table>
				</a></td></tr></table></div>';
	}

	function openBox($class) {
		return '
					<table class="'.$class.'" width="100%" border="0" cellspacing="0" cellpadding="0">
						<tr height="20">
							<td class="box-top-left"></td>
							<td class="box-top"></td>
							<td class="box-top-right"></td>
						</tr>
				';
	}
		
	function boxTitle($title, $link=NULL, $id=NULL) {
		$row = '<tr><td class="box-left"></td><th valign="top">';
		
		if ($link) {
			$row .= '<a href="'.$link.'">';
		}
		
		$row .= '<div';
		if ($id) {
			$row .= ' id="'.$id.'"';
		}
		
		$row .= ' class="box-title">'.$title.'</div>';

		if ($link) {
			$row .= '</a>';
		}
				
		$row .= '</th><td class="box-right"></td></tr>';
		return $row;
	}
	
	function openBoxRow($height = NULL) {
		if ($height) {
			return '
						<tr height="$height">
							<td class="box-left"></td>
							<td valign="top">
				';
		} else {
			return '
						<tr>
							<td class="box-left"></td>
							<td valign="top">
				';
		}
	}
	
	function closeBoxRow() {
		return '
							</td>
							<td class="box-right"></td>
						</tr>
				';
	}
	
	function boxDivider() {
		return '
						<tr height="9">
							<td class="box-left-divider"></td>
							<td class="box-divider"></td>
							<td class="box-right-divider"></td>
						</tr>
				';
	}
	
	function closeBox() {
		return '
						<tr height="20">
							<td class="box-bottom-left"></td>
							<td class="box-bottom"></td>
							<td class="box-bottom-right"></td>
						</tr>
					</table>
				';
	}
		
	function drawPublication($publication, $db) {
		$title = rtrim($publication["title"], ".");
		echo('<div class="box-subtitle">
			<a href="/publications.html?id='.$publication["id"].'">'.$title . '</a>
			</div>
			<div class="box-body">');
		$authors1 = explode("&", $publication["authors"]);
		$authors = explode(",", $authors1[0]);
		if (sizeof($authors1) > 1) {
			array_push($authors, $authors1[1]);
		}
					
		$n = sizeof($authors);
		for ($i=0; $i < $n; $i++) {
			$author = trim($authors[$i]);
			list($name, $initials) = explode(" ", $author);
			$result = mysql_query("SELECT * FROM People WHERE lastName=\"$name\" AND initials=\"$initials\"",$db);
			$person = mysql_fetch_array($result);
			if ($person) {
				echo('<b><a href="/people.html?id='.$person[id].'">'.$authors[$i].'</a></b>');				
			} else {
				echo($author);
			}
			
			if ($i == ($n - 2)) {
				echo(' & ');
			} else if ($i < ($n - 2)) {
				echo(', ');
			} 
		}
		printf(" (%s) <i>%s</i> <b>%s</b>, %s.</div>\n", 
				$publication["year"], $publication["journal"], $publication["volume"], 
				$publication["pages"]);
	}

/*	
	function drawPublication($publication) {
	
		$title = rtrim($publication["title"], ".");
		printf("<a href=\"/publications.html?id=%s\">".
				"<div class=\"box-subtitle\">%s.</div>".
				"<div class=\"box-body\">%s (%s) <i>%s</i> <b>%s</b>, %s.</div></a>\n", 
				$publication["id"], $title, $publication["authors"], 
				$publication["year"], $publication["journal"], $publication["volume"], 
				$publication["pages"]);
	}
*/	
	function emailAddress($email) {
		if ($email == NULL) {
			return "Unknown email address";
		}
		$address = explode("@", $email);
		if (sizeof($address) != 2) {
			return "Bad email address";
		}
		return '<script type="text/javascript"><!--
					document.write( "'.$address[0].'" );
					document.write( "@" );
					document.write( "'.$address[1].'" );
				--></script>';
	}
	
	function errorPage($message, $email) {
		echo('<html>
			<head>
				<title>Oxford Evolutionary Biology - Error</title>
				<link href="evolve.css" rel="stylesheet" media="screen">
			</head>
			<body>
				<h3>'.$message.'</h3>
				<h4>Please contact: '.emailAddress($email).'</h4>
			</body>
			</html>');
	}
	
?>